#include "StdAfx.h"
#include "dll_object.h"
#include <interface/interface.h>
// #include <interface/xml_parser_interface.h>
// #include <interface/log_interface.h>
// #include <utility/api_tools.h>
// #include <Util/WinAPITools.h>
#include "session_impl.h"

#pragma warning(disable:4996)

aot::tt::imain*				dll_object::im_ = NULL;
dll_object* g_dll_obj = NULL;

dll_object::dll_object()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	this->im_ = NULL;
	this->ptr_ft_client_ = NULL;
}

dll_object::~dll_object()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	if ( this->ptr_ft_client_ )
	{
		delete this->ptr_ft_client_;
		this->ptr_ft_client_ = NULL;
	}
}

void 
dll_object::destroy()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if( 0 == dec_ref() )
		delete this;
}

bool 
dll_object::on_regist(aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	this->parent_wnd_ = p->parent_wnd;

	this->im_ = p->mi;
	this->global_.init(this->im_);
	this->i_wrapper_.init_component_query(this->im_, this);

	return true;
}

int	
dll_object::on_putq (aot::tt::message_t* msg, long tm, aot::tt::result_t* ret)
{
	return -1;
}

int	
dll_object::on_command (aot::tt::command_t* cmd, aot::tt::result_t* ret)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	return -1;
}

bool 
dll_object::query_interface(void** out, const char* key)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	if( 0 == strcmp("ip2pft_client", key) ) 
	{
		if ( NULL == this->ptr_ft_client_ )
		{
			this->ptr_ft_client_ = new p2pft_client_impl();
		}
		*out = this->ptr_ft_client_;
		return true;
	}

	return false;
}

global_data_wrapper* get_global()
{
	return &g_dll_obj->global_;
}

interface_wrapper* get_interface()
{
	return &g_dll_obj->i_wrapper_;
}
